package cn.ittaoli.szdz.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by lub_lenovo on 2017/11/10.
 */
public class LoginInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String requestURI = httpServletRequest.getRequestURI();
        //如果是登录页面则放行
        if(requestURI.indexOf("login")>=0){
            return true;
        }
        if(requestURI.indexOf("css")>=0){
            return true;
        }

        if(requestURI.indexOf("js")>=0){
            return true;
        }
        if(requestURI.indexOf("image")>=0){
            return true;
        }
        if(requestURI.indexOf("ttf")>=0){
            return true;
        }
        if(requestURI.indexOf("woff")>=0){
            return true;
        }

        HttpSession session = httpServletRequest.getSession();

        //如果用户未登录不放行
        if(session.getAttribute("user")==null){
            //用户没有登录挑战到登录页面
            httpServletRequest.getRequestDispatcher("/index.jsp").forward(httpServletRequest, httpServletResponse);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
